//
//  XQStatisticsViewController.m
//  XQBluetoothSDK
//
//  Created by 包月兴 on 16/7/19.
//  Copyright © 2016年 包月兴. All rights reserved.
//

#import "XQStatisticsViewController.h"
#import "AKTKit.h"
#import "XQStepModel.h"
#import "XQSleepModel.h"
#import "XQHeartModel.h"
#import "JSONKit.h"
#import "NSDate+XQExpend.h"
#import "XQBluetoothSDK.h"
@interface XQStatisticsViewController ()
@property (nonatomic,copy) UILabel *textField;
@property (nonatomic,copy) UIButton *dayDetailsBtn; //日详情按钮
@property (nonatomic,copy) UIButton *stepWeekCountBtn; //步数周统计
@property (nonatomic,copy) UIButton *stepMonthCountBtn; //步数月统计
@property (nonatomic,copy) UIButton *sleepDayCountBtn; //睡眠日统计
@property (nonatomic,copy) UIButton *sleepWeekCountBtn; //睡眠周统计
@property (nonatomic,copy) UIButton *sleepMonthCountBtn; //睡眠月统计
@property (nonatomic,copy) UIButton *heartDetailsBtn; //心率详情按钮

@end

@implementation XQStatisticsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _textField = self.textField;
    _dayDetailsBtn = self.dayDetailsBtn;
    _stepWeekCountBtn = self.stepWeekCountBtn;
    _stepMonthCountBtn = self.stepMonthCountBtn;
    _sleepDayCountBtn = self.sleepDayCountBtn;
    _sleepWeekCountBtn = self.sleepWeekCountBtn;
    _sleepMonthCountBtn = self.sleepMonthCountBtn;
    _heartDetailsBtn = self.heartDetailsBtn;
    self.title = @"运动睡眠详情";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(UILabel *)textField
{
    if (_textField == nil) {
        _textField = [[UILabel alloc] initWithFrame:CGRectMake(0,0,mAKT_SCREENWITTH, mAKT_SCREENHEIGHT/3)];
        _textField.backgroundColor = mAKT_Color_Yellow;
        _textField.font = mAKT_Font_12;
        _textField.textColor = mAKT_Color_LightGray;
        _textField.numberOfLines = 0;
        [self.view addSubview:_textField];
    }
    return _textField;
}

-(UIButton *)dayDetailsBtn
{
    if (_dayDetailsBtn == nil) {
        _dayDetailsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (mAKT_SCREENWITTH/2 - 150)/2;
        _dayDetailsBtn.frame  = CGRectMake(x,mAKT_SCREENHEIGHT/3+ 10,150,20);
        _dayDetailsBtn.layer.cornerRadius = 10;
        _dayDetailsBtn.backgroundColor = [UIColor lightGrayColor];
        [_dayDetailsBtn setTitle:@"今日运动详情" forState:UIControlStateNormal];
        [_dayDetailsBtn addTarget:self action:@selector(dayDetailsEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_dayDetailsBtn];
    }
    return _dayDetailsBtn;
}
-(UIButton *)stepWeekCountBtn
{
    if (_stepWeekCountBtn == nil) {
        _stepWeekCountBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (mAKT_SCREENWITTH/2 - 150)/2 + mAKT_SCREENWITTH/2;
        _stepWeekCountBtn.frame  = CGRectMake(x,mAKT_SCREENHEIGHT/3+ 10,150,20);
        _stepWeekCountBtn.layer.cornerRadius = 10;
        _stepWeekCountBtn.backgroundColor = [UIColor lightGrayColor];
        [_stepWeekCountBtn setTitle:@"本周运动统计" forState:UIControlStateNormal];
        [_stepWeekCountBtn addTarget:self action:@selector(stepWeekCountEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_stepWeekCountBtn];
    }
    return _stepWeekCountBtn;
}

-(UIButton *)stepMonthCountBtn
{
    if (_stepMonthCountBtn == nil) {
        _stepMonthCountBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (mAKT_SCREENWITTH/2 - 150)/2;
        _stepMonthCountBtn.frame  = CGRectMake(x,mAKT_SCREENHEIGHT/3+ 50,150,20);
        _stepMonthCountBtn.layer.cornerRadius = 10;
        _stepMonthCountBtn.backgroundColor = [UIColor lightGrayColor];
        [_stepMonthCountBtn setTitle:@"本月运动统计" forState:UIControlStateNormal];
        [_stepMonthCountBtn addTarget:self action:@selector(stepMonthCountEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_stepMonthCountBtn];
    }
    return _stepMonthCountBtn;
}

-(UIButton *)sleepDayCountBtn
{
    if (_sleepDayCountBtn == nil) {
        _sleepDayCountBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (mAKT_SCREENWITTH/2 - 150)/2+mAKT_SCREENWITTH/2;
        _sleepDayCountBtn.frame  = CGRectMake(x,mAKT_SCREENHEIGHT/3+ 50,150,20);
        _sleepDayCountBtn.layer.cornerRadius = 10;
        _sleepDayCountBtn.backgroundColor = [UIColor lightGrayColor];
        [_sleepDayCountBtn setTitle:@"今日睡眠统计" forState:UIControlStateNormal];
        [_sleepDayCountBtn addTarget:self action:@selector(sleepDayCountEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_sleepDayCountBtn];
    }
    return _sleepDayCountBtn;
}

-(UIButton *)sleepWeekCountBtn
{
    if (_sleepWeekCountBtn == nil) {
        _sleepWeekCountBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (mAKT_SCREENWITTH/2 - 150)/2;
        _sleepWeekCountBtn.frame  = CGRectMake(x,mAKT_SCREENHEIGHT/3+ 90,150,20);
        _sleepWeekCountBtn.layer.cornerRadius = 10;
        _sleepWeekCountBtn.backgroundColor = [UIColor lightGrayColor];
        [_sleepWeekCountBtn setTitle:@"本周睡眠统计" forState:UIControlStateNormal];
        [_sleepWeekCountBtn addTarget:self action:@selector(sleepWeekCountEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_sleepWeekCountBtn];
    }
    return _sleepWeekCountBtn;
}

-(UIButton *)sleepMonthCountBtn
{
    if (_sleepMonthCountBtn == nil) {
        _sleepMonthCountBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (mAKT_SCREENWITTH/2 - 150)/2+mAKT_SCREENWITTH/2;
        _sleepMonthCountBtn.frame  = CGRectMake(x,mAKT_SCREENHEIGHT/3+ 90,150,20);
        _sleepMonthCountBtn.layer.cornerRadius = 10;
        _sleepMonthCountBtn.backgroundColor = [UIColor lightGrayColor];
        [_sleepMonthCountBtn setTitle:@"本月睡眠统计" forState:UIControlStateNormal];
        [_sleepMonthCountBtn addTarget:self action:@selector(sleepMonthCountEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_sleepMonthCountBtn];
    }
    return _sleepMonthCountBtn;
}


-(UIButton *)heartDetailsBtn
{
    if (_heartDetailsBtn == nil) {
        _heartDetailsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        CGFloat x= (mAKT_SCREENWITTH/2 - 150)/2;
        _heartDetailsBtn.frame  = CGRectMake(x,mAKT_SCREENHEIGHT/3+ 140,150,20);
        _heartDetailsBtn.layer.cornerRadius = 10;
        _heartDetailsBtn.backgroundColor = [UIColor lightGrayColor];
        [_heartDetailsBtn setTitle:@"今日心率统计" forState:UIControlStateNormal];
        [_heartDetailsBtn addTarget:self action:@selector(heartDetailsEvent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_heartDetailsBtn];
    }
    return _heartDetailsBtn;
}


-(void)dayDetailsEvent
{
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeInterval time = [zone secondsFromGMTForDate:date];
    NSDate *dateNow = [date dateByAddingTimeInterval:time];
    NSInteger now = [dateNow timeIntervalSince1970];
    NSInteger day = [[NSDate ISO8601StringWithFormat:DateFormatTypeOne timeInterval:now] integerValue];
    [XQStepModel gainSportDetailWithDate:day compeletion:^(NSArray<XQStepModel *> *steps, NSInteger totalSteps, CGFloat calorie, CGFloat mileage, NSInteger sprotDuration) {
        NSMutableDictionary *details = [NSMutableDictionary dictionary];
        for (XQStepModel *model in steps) {
            NSInteger hour = model.start_ts/3600;
            NSInteger min = (model.start_ts%3600)/60;
            NSString *key = [NSString stringWithFormat:@"%02ld:%02ld",(long)hour,min];
           [details setObject:[NSString stringWithFormat:@"%ld步",(long)model.steps] forKey:key];
            
        }
        NSDictionary *dic = [NSDictionary dictionary];
        if (steps.count > 0) {
             dic= @{@"运动详情":details,@"总步数":@(totalSteps),@"消耗卡路里":@(calorie),@"里程":@(mileage),@"运动时长":@(sprotDuration)};
        }else
        {
             dic= @{@"运动详情":@"暂无数据",@"总步数":@(totalSteps),@"消耗卡路里":@(calorie),@"里程":@(mileage),@"运动时长":@(sprotDuration)};
        }
       
        NSString *text= [dic JSONString];
        XQBLELog(@"今日运动详情##################:%@",text);
        _textField.text = text;
    }];
}

-(void)stepWeekCountEvent
{
    [XQStepModel gainWeekCountDetailWithSerial:0 compeletion:^(NSInteger averageSteps, CGFloat averageCalorie, CGFloat averageMileage, CGFloat averageSprotDuration, NSString *description,NSInteger totalDay) {
        NSDictionary *dic= @{@"周均运动步数":@(averageSteps),@"周均里程":@(averageMileage),@"周均消耗卡路里":@(averageCalorie),@"周均运动时长":@(averageSprotDuration),@"描述统计时段":description};
        NSString *text= [dic JSONString];
        _textField.text = text;
    }];
}

-(void)stepMonthCountEvent
{
    [XQStepModel gainMonthCountDetailWithSerial:0 compeletion:^(NSInteger averageSteps, CGFloat averageCalorie, CGFloat averageMileage, CGFloat averageSprotDuration, NSString *description,NSInteger totalDay) {
        NSDictionary *dic= @{@"月均运动步数":@(averageSteps),@"月均里程":@(averageMileage),@"月均消耗卡路里":@(averageCalorie),@"月均运动时长":@(averageSprotDuration),@"描述统计时段":description};
        NSString *text= [dic JSONString];
        _textField.text = text;
    }];
}

-(void)sleepDayCountEvent
{
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeInterval time = [zone secondsFromGMTForDate:date];
    NSDate *dateNow = [date dateByAddingTimeInterval:time];
    NSInteger now = [dateNow timeIntervalSince1970];
    NSInteger day = [[NSDate ISO8601StringWithFormat:DateFormatTypeOne timeInterval:now] integerValue];
    
    [XQSleepModel gainSleepDetailWithDate:day compeletion:^(NSArray<XQSleepModel *>*sleeps,NSInteger deepDuration,NSInteger simpleDuration,NSInteger startTime,NSInteger getUpTime,NSInteger sleepHeart) {
        NSMutableDictionary *details = [NSMutableDictionary dictionary];
        for ( XQSleepModel *model in sleeps) {
            NSInteger hour = model.start_ts/3600;
            NSInteger min = (model.start_ts%3600)/60;
            NSString *key = [NSString stringWithFormat:@"%02ld:%02ld",(long)hour,min];
            switch (model.quality) {
                case XQSleepQualityDeep:
                    [details setObject:@"深睡" forKey:key];
                    break;
                case XQSleepQualitySimple:
                    [details setObject:@"浅睡" forKey:key];
                    break;
                default:
                    break;
            }
        }
        
        NSDictionary *dic = [NSDictionary dictionary];
        if (sleeps.count > 0) {
            dic = @{@"睡眠详情":details,@"深睡时长":@(deepDuration),@"浅睡时长":@(simpleDuration)};
        }else
        {
            dic = @{@"睡眠详情":@"暂无数据",@"深睡时长":@(deepDuration),@"浅睡时长":@(simpleDuration)};
        }
        
        NSString *text= [dic JSONString];
        XQBLELog(@"今日睡眠详情##################:%@",text);
        _textField.text = text;
    }];
}

-(void)sleepWeekCountEvent
{
    
    [XQSleepModel gainWeekCountDetailWithSerial:0 compeletion:^(NSInteger averageDeepDuration, NSInteger averageSimpleDuration, NSString *description,NSInteger totalDay) {
        NSDictionary *dic = @{@"周均深度睡眠时长":@(averageDeepDuration),@"周均浅度睡眠时长":@(averageSimpleDuration),@"描述统计时段":description};
        NSString *text= [dic JSONString];
        _textField.text = text;
    }];
}

-(void)sleepMonthCountEvent
{
    [XQSleepModel gainMonthCountDetailWithSerial:0 compeletion:^(NSInteger averageDeepDuration, NSInteger averageSimpleDuration, NSString *description,NSInteger totalDay) {
        NSDictionary *dic = @{@"月均深度睡眠时长":@(averageDeepDuration),@"月均浅度睡眠时长":@(averageSimpleDuration),@"描述统计时段":description};
        NSString *text= [dic JSONString];
        _textField.text = text;
    }];
}


-(void)heartDetailsEvent
{
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeInterval time = [zone secondsFromGMTForDate:date];
    NSDate *dateNow = [date dateByAddingTimeInterval:time];
    NSInteger now = [dateNow timeIntervalSince1970];
    NSInteger day = [[NSDate ISO8601StringWithFormat:DateFormatTypeOne timeInterval:now] integerValue];
    [XQHeartModel gainHeartDetailWithDate:day compeletion:^(NSArray<XQHeartModel *> *hearts) {
        NSMutableDictionary *details = [NSMutableDictionary dictionary];
        for (XQHeartModel *model in hearts) {
            NSInteger hour = model.ts/3600;
            NSInteger min = (model.ts%3600)/60;
            NSString *key = [NSString stringWithFormat:@"%02ld:%02ld",(long)hour,min];
            [details setObject:[NSString stringWithFormat:@"心率-%ld",(long)model.heart] forKey:key];
        }
        NSString *text= [details JSONString];
        XQBLELog(@"今日心率详情##################:%@",text);
        _textField.text = text;
    }];
    
    
}


@end
